<?php

/*
 * 2021/7/16
 * 20:28
 * User: 2650332702@qq.com
 */
namespace App\Api\Models;


use Illuminate\Database\Eloquent\Model;

class CmcPromotionPintuanGroup extends Model
{
    protected $table = 'cmc_promotion_pintuan_group';

    //查询状态拼团详情
    static public function judgment($uid,$allow_field,$status=2)
    {
        return CmcPromotionPintuanGroup::where('group_id',$uid)->where('status',$status)->get($allow_field)->toArray();
    }

    //查询拼团列表
    static public function pintuanList(int $id,array $allow_field)
    {
        return CmcPromotionPintuanGroup::where('id',$id)->get($allow_field)->toArray();
    }

    //获得所有拼团组
    static public function getPidAll(int $pid,array $allow_field)
    {
        return CmcPromotionPintuanGroup::where('pintuan_id',$pid)->get($allow_field)->toArray();
    }

    //图片访问器
    public function getHeadMemberImgAttribute($value)
    {
        return config('aliyun.cui').$this->attributes['head_member_img'];
    }

    //获取当前拼团组
    public static function pinkInfo($id)
    {
        return self::where('group_id',$id)->count();
    }
    // 获取当前数量
    public static function getPintuanCount($id)
    {
        $id = self::pinkInfo($id);
        return self::where('group_id',$id)->get('pintuan_count')->toArray()[0]['pintuan_count'];
    }
    // 获取拼团数量
    public static function getPintuanNum($id)
    {
        $id = self::pinkInfo($id);
        return self::where('group_id',$id)->get('pintuan_num')->toArray()[0]['pintuan_num'];
    }
    // 当前数量+1
    public static function add_join_count($id)
    {
        $id = self::pinkInfo($id);
        $pintuan_count = self::where('group_id', $id)->get('pintuan_count')->toArray()[0]['pintuan_count'] + 1;
        // 将对应的参与拼团人数+1
        return CmcPromotionPintuanGroup::where('group_id', $id)->update(['pintuan_count' => $pintuan_count]);
    }
    //查询拼团信息
    static public function getPintuanData($group)
    {
        return CmcPromotionPintuanGroup::insert($group);
    }

    //查询用户是否已经拼团
    static public function isPintuan($uid,$pid,$status)
    {
        return CmcPromotionPintuanGroup::where('pintuan_id',$pid)->where('head_id',$uid)->where('status',$status)->count();

    }
}
